package cn.lotusnut.rabbitmqbaseapi.mq.topic;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author JiangMing Zhou
 * @Date 2022/11/30 21:24
 * @Version 1.8
 */
public class ConsumeB {

    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("47.108.157.114");
        connectionFactory.setVirtualHost("/");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("511923ZjM5219");
        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            channel.exchangeDeclare("topic_exchange", BuiltinExchangeType.TOPIC);
            channel.queueDeclare("age", true, false, false, null);
            channel.queueBind("age", "topic_exchange", "age.#");
            channel.basicConsume("age", true, new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println(new String(body) + "发送邮件");
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TimeoutException e) {
            e.printStackTrace();
        }
    }
}
